<?php
/**
 * Created by : PhpStorm
 * Message: 后台首页控制器
 * User: bingdi
 * Time: 2020/2/1814:42
 * motto: 现在的努力是为了小时候吹过的牛逼!
 */

namespace app\admin\controller;


use app\BaseController;
use think\cache\driver\Redis;
use think\facade\Db;
use think\facade\View;
use think\route\dispatch\Controller;

class Index extends AdminBase
{
    public function index(){
        return View::fetch();
    }
    public function welcome(){
        //书籍库数量
        $user_num=Db::name('user')
            ->count();
        $today_user_num=Db::name('user')
            ->whereTime('create_time','today')->count();
        $yesterday_user_num=Db::name('user')
            ->whereTime('create_time','yesterday')->count();
        $vip_num=Db::name('vip_order')
            ->where('status',1)
            ->count();
        $today_vip_num=Db::name('vip_order')
            ->where('status',1)
            ->whereTime('create_time','today')->count();
        $yesterday_vip_num=Db::name('vip_order')
            ->where('status',1)
            ->whereTime('create_time','yesterday')->count();
        $smoke_num=Db::name('smoke_record')
            ->count();
        $today_smoke_num=Db::name('smoke_record')
            ->whereTime('create_time','today')->count();
        $yesterday_smoke_num=Db::name('smoke_record')
            ->whereTime('create_time','yesterday')->count();
        $cost_num=Db::name('smoke_cost_record')
            ->count();
        $today_cost_num=Db::name('smoke_cost_record')
            ->whereTime('create_time','today')->count();
        $yesterday_cost_num=Db::name('smoke_cost_record')
            ->whereTime('create_time','yesterday')->count();


        //vip数据
        $vip_user_num=Db::name('user')
            ->where('is_vip',2)
            ->count();
        //月度会员
        $yongjiuvip= Db::name('user')
            ->whereTime('vip_end_time','>=',4002329600)
            ->where('is_vip',2)
            ->count();
        //用户设备
        $apple=Db::name('user')
            ->where('device_type','苹果')
            ->count();
        $anzhuo=Db::name('user')
            ->where('device_type','安卓')
            ->count();
        $xiaochengxu=Db::name('user')
            ->where('device_type',"小程序")
            ->count();
        $data['user_num']=$user_num;
        $data['today_user_num']=$today_user_num;
        $data['yesterday_user_num']=$yesterday_user_num;

        $data['vip_num']=$vip_num;
        $data['today_vip_num']=$today_vip_num;
        $data['yesterday_vip_num']=$yesterday_vip_num;

        $data['smoke_num']=$smoke_num;
        $data['today_smoke_num']=$today_smoke_num;
        $data['yesterday_smoke_num']=$yesterday_smoke_num;

        $data['cost_num']=$cost_num;
        $data['today_cost_num']=$today_cost_num;
        $data['yesterday_cost_num']=$yesterday_cost_num;

        $data['vip_user_num']=$vip_user_num;
        $data['yongjiuvip']=$yongjiuvip;
        $data['apple']=$apple;
        $data['anzhuo']=$anzhuo;
        $data['xiaochengxu']=$xiaochengxu;

        return View::fetch('welcome',['data'=>$data]);
    }
    /**\
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 接口返回
     */
    public function permission_api(){
        $menu=Db::name('menu_role')
            ->where('role_id',$this->adminUser['role_id'])
            ->column('menu_id');
        $model=Db::name('menu')
            ->where(['parent_id'=>0])
            ->where(['id'=>$menu])
            ->select()
            ->toArray();
        foreach($model as $k=>$v){
            $model[$k]['subMenus']=Db::name('menu')
                ->where(['parent_id'=>$v['id']])
                ->where(['id'=>$menu])
                ->select()
                ->toArray();
        }
        return show(1,'请求成功',$model);
    }

}